/*
 *  $Id: FlyingShroud.cs 1190 2010-11-12 15:02:56Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;

using AgateLib;
using AgateLib.Geometry;

using Ragima.Helper;

namespace Ragima.Menu {
    class FlyingShroud {
        private const int MinAlpha = 150;
        private const int MaxAlpha = 255;

        private PointF _point, _target;
        private int _alpha;
        private bool _alphaDir;

        public FlyingShroud(bool fullscreen) {
            _point = fullscreen ? FullRandomPoint() : BorderRandomPoint();
            _alpha = Randomizer.Global.Next(MinAlpha, MaxAlpha);
            _alphaDir = true;
            NewTarget();
        }

        public void Draw() {
            Data.Shroud.Color = Color.FromArgb(_alpha, Color.White);
            Data.Shroud.Draw(_point);
        }

        public void Process() {
            if (_alphaDir) {
                if (_alpha == MaxAlpha) _alphaDir = false; else _alpha++;
            } else {
                if (_alpha == MinAlpha) _alphaDir = true; else _alpha--;
            }
            if (_point.ApproxEquals(_target)) NewTarget();
            double dx = _target.X - _point.X, dy = _target.Y - _point.Y;
            double length = Math.Sqrt(dx*dx + dy*dy);
            _point.X += (float) (dx / length / 5);
            _point.Y += (float) (dy / length / 5);
        }

        private void NewTarget() {
            double dx, dy;
            do {
                _target = FullRandomPoint();
                dx = Math.Abs(_target.X - _point.X);
                dy = Math.Abs(_target.Y - _point.Y);
            } while (dx < dy*3);
        }

        private const int WidthPart1 = Constants.DisplayWidth / 8;
        private const int WidthPart2 = Constants.DisplayWidth - Constants.DisplayWidth / 8;
        private const int HeightPart1 = Constants.DisplayHeight / 8;
        private const int HeightPart2 = Constants.DisplayHeight - Constants.DisplayHeight / 8;

        private static PointF FullRandomPoint() {
            Randomizer r = Randomizer.Global;
            return new PointF(r.Next(Constants.DisplayWidth), r.Next(Constants.DisplayHeight));
        }
        private static PointF BorderRandomPoint() {
            PointF point = PointF.Empty;
            Randomizer r = Randomizer.Global;
            switch (r.Next(4)) {
                case 0:
                    point = new PointF(r.Next(Constants.DisplayWidth), r.Next(HeightPart1));
                    break;
                case 1:
                    point = new PointF(r.Next(Constants.DisplayWidth), r.Next(HeightPart2, Constants.DisplayHeight));
                    break;
                case 2:
                    point = new PointF(r.Next(WidthPart1), r.Next(Constants.DisplayHeight));
                    break;
                case 3:
                    point = new PointF(r.Next(WidthPart2, Constants.DisplayWidth), r.Next(Constants.DisplayHeight));
                    break;
            }
            return point;
        }
    }
}